﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace AgileShare.Framework.Publishing.Helpers
{
    /// <summary>
    /// This class contains helper methods for working with SharePoint Publishing Webs
    /// - All methods should be static
    /// - DO NOT USE SPContext
    /// </summary>
    public static class PublishingWebHelper
    {
		/// <summary>
		/// Configures the available page layouts in a specific publishing web.
		/// </summary>
		/// <param name="web">The publishing web to setup.</param>
		/// <param name="pageLayoutIds">The IDs of the page layouts that will be available in the web. 
		/// The first page layout ID will be configured as the default page layout for the web.</param>
		public static void ConfigureAvailablePageLayouts(SPWeb web, params string[] pageLayoutIds)
		{
			List<string> pageLayoutIdsList = pageLayoutIds.ToList();

			web.AllowUnsafeUpdates = true;

			PublishingSite pSite = new PublishingSite(web.Site);
			PageLayoutCollection sitePageLayouts = pSite.PageLayouts;
			var pageLayouts = sitePageLayouts.Where(x => pageLayoutIdsList.Contains(x.Name)).ToArray();

			PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
			pWeb.SetAvailablePageLayouts(pageLayouts, false);

			PageLayout defaultPageLayout = pSite.PageLayouts.FirstOrDefault(x => x.Name == pageLayoutIdsList[0]);
			pWeb.SetDefaultPageLayout(defaultPageLayout, false);

			pWeb.Update();
		}

		/// <summary>
		/// Sets the custom master page.
		/// </summary>
		/// <param name="customMasterPageFileName">Name of the master page file.</param>
		/// <param name="web">The web.</param>
		public static void SetCustomMasterPage(string customMasterPageFileName, SPWeb web)
		{
			web.CustomMasterUrl = GetFullMasterPagePath(customMasterPageFileName, web.Site.RootWeb);
			web.Update();
		}

		/// <summary>
		/// Sets the system master page.
		/// </summary>
		/// <param name="systemMasterPageFileName">Name of the master page file.</param>
		/// <param name="web">The web.</param>
		public static void SetSystemMasterPage(string systemMasterPageFileName, SPWeb web)
		{
			web.MasterUrl = GetFullMasterPagePath(systemMasterPageFileName, web.Site.RootWeb);
			web.Update();
		}

		/// <summary>
		/// Sets the master pages.
		/// </summary>
		/// <param name="customMasterPageFileName">Name of the custom master page file.</param>
		/// <param name="systemMasterPageFileName">Name of the system master page file.</param>
		/// <param name="web">The web.</param>
		public static void SetMasterPages(string customMasterPageFileName, string systemMasterPageFileName, SPWeb web)
		{

			web.CustomMasterUrl = GetFullMasterPagePath(customMasterPageFileName, web.Site.RootWeb);
			web.MasterUrl = GetFullMasterPagePath(systemMasterPageFileName, web.Site.RootWeb);
			web.Update();			
		}

		/// <summary>
		/// Gets the full master page path.
		/// </summary>
		/// <param name="fileName">Name of the file.</param>
		/// <param name="web">The web.</param>
		/// <returns></returns>
		public static string GetFullMasterPagePath(string fileName, SPWeb web)
		{
			if (!fileName.Contains("/_catalogs/masterpage/"))
			{
				return SPUrlUtility.CombineUrl(web.Site.RootWeb.ServerRelativeUrl, "/_catalogs/masterpage/" + fileName);
			}
			else
			{
				return fileName;
			}
		}
    }
}
